# Contributor: Stuart Cardall <developer@it-offshore.co.uk>
# Contributor: Ash Berlin-Taylor <ash.berlin@gmail.com>
# Contributor: Valery Kartel <valery.kartel@gmail.com>
# Contributor: Łukasz Jendrysik <scadu@yandex.com>
# Contributor: Jesse Young <jlyo@jlyo.org>
# Maintainer: Celeste <cielesti@protonmail.com>
pkgname=exim
pkgver=4.97.1
pkgrel=0
pkgdesc="Message Transfer Agent"
url="https://www.exim.org/"
arch="all"
license="GPL-2.0-or-later"
options="!check suid"
pkgusers="$pkgname"
pkggroups="$pkgname mail"
depends="ca-certificates"
makedepends="bash tdb-dev gawk libidn-dev libspf2-dev linux-headers
	mariadb-connector-c-dev openssl-dev>3 pcre2-dev perl
	perl-file-fcntllock libpq-dev sqlite-dev
	"
install="exim.pre-install"
subpackages="$pkgname-cdb $pkgname-dbmdb $pkgname-dnsdb $pkgname-doc
	$pkgname-mysql $pkgname-openrc $pkgname-postgresql
	$pkgname-scripts::noarch $pkgname-sqlite $pkgname-utils
	"
source="https://ftp.exim.org/pub/exim/exim4/exim-$pkgver.tar.xz
	bounce-charset.patch
	dnsdb-multi-chunk.patch
	exim.Makefile
	exim.confd
	exim.initd
	exim.logrotate
	exim.gencert
	"

# secfixes:
#   4.97.1-r0:
#     - CVE-2023-51766
#   4.96.2-r0:
#     - CVE-2023-42117
#     - CVE-2023-42119
#   4.96.1-r0:
#     - CVE-2023-42114
#     - CVE-2023-42115
#     - CVE-2023-42116
#   4.94.2-r0:
#     - CVE-2021-27216
#     - CVE-2020-28007
#     - CVE-2020-28008
#     - CVE-2020-28009
#     - CVE-2020-28010
#     - CVE-2020-28011
#     - CVE-2020-28012
#     - CVE-2020-28013
#     - CVE-2020-28014
#     - CVE-2020-28015
#     - CVE-2020-28016
#     - CVE-2020-28017
#     - CVE-2020-28018
#     - CVE-2020-28019
#     - CVE-2020-28020
#     - CVE-2020-28021
#     - CVE-2020-28022
#     - CVE-2020-28023
#     - CVE-2020-28024
#     - CVE-2020-28025
#     - CVE-2020-28026
#   4.93-r1:
#     - CVE-2020-12783
#   4.92.2-r1:
#     - CVE-2019-16928
#   4.92.2-r0:
#     - CVE-2019-15846
#   4.92.1-r0:
#     - CVE-2019-13917
#   4.92-r0:
#     - CVE-2019-10149
#   4.89-r5:
#     - CVE-2017-1000369
#   4.89-r7:
#     - CVE-2017-16943
#   4.89.1-r0:
#     - CVE-2017-16944
#   4.90.1-r0:
#     - CVE-2018-6789

prepare() {
	default_prepare

	cp "$srcdir"/$pkgname.Makefile Local/Makefile
	sed -i \
		-e 's/-lnsl//g' \
		-e 's/^HAVE_ICONV.*$//' \
		OS/Makefile-Linux
}

build() {
	export CFLAGS="$CFLAGS -DNO_EXECINFO"
	make makefile
	make
}

package() {
	install -m750 -D -g mail -d "$pkgdir"/etc/mail
	make DESTDIR="$pkgdir" INSTALL_ARG="-no_symlink -no_chown exim" install
	install -D -m644 doc/exim.8 "$pkgdir"/usr/share/man/man8/exim.8
	cd "$pkgdir"/usr/sbin
	mv exim-${pkgver%.*}* exim
	chmod u+s exim
	local i; for i in mailq rmail rsmtp runq sendmail newaliases; do
		ln -s exim $i
	done
	install -Dm644 "$srcdir"/$pkgname.logrotate "$pkgdir"/etc/logrotate.d/$pkgname
	install -Dm644 "$srcdir"/$pkgname.confd "$pkgdir"/etc/conf.d/$pkgname
	install -Dm755 "$srcdir"/$pkgname.initd "$pkgdir"/etc/init.d/$pkgname
	# Fix clamav local socket path, Add variant to spamd address
	sed -i \
		-e 's~# av_scanner = clamd:/tmp/clamd~# av_scanner = clamd:/run/clamav/clamd.sock~' \
		-e '/# spamd_address = 127.0.0.1 783/a# spamd_address = 127.0.0.1 11333 variant=rspamd' \
		"$pkgdir"/etc/$pkgname/$pkgname.conf
	# Create subdirs for logs and extensions
	install -dm750 -o $pkgname -g mail "$pkgdir"/var/log/$pkgname
	mkdir -p "$pkgdir"/usr/lib/$pkgname
}

scripts() {
	pkgdesc="EXIM scripts"
	depends="$pkgname perl perl-file-fcntllock"
	cd "$builddir"
	make	DESTDIR="$subpkgdir" \
		INSTALL_ARG="exicyclog exim_checkaccess eximstats exiqgrep exigrep exinext exiqsumm exipick exiwhat convert4r3 convert4r4 exim_msgdate exim_id_update" \
		install
	rm -fr "${subpkgdir:?}"/etc
}

utils() {
	pkgdesc="EXIM utils"
	depends="$pkgname"
	cd "$builddir"
	make	DESTDIR="$subpkgdir" \
		INSTALL_ARG="exim_dbmbuild exim_dumpdb exim_tidydb exim_fixdb exim_lock" \
		install
	install -m755 "$srcdir"/exim.gencert "$subpkgdir"/usr/sbin/exim_gencert
	rm -fr "${subpkgdir:?}"/etc
}

_mv_ext() {
	pkgdesc="EXIM extension: $1"
	depends="$pkgname"
	install -D -m755 "$builddir"/build-Linux-*/lookups/$1.so "$subpkgdir"/usr/lib/$pkgname/$1.so
}

sqlite() { _mv_ext sqlite; }
mysql() { _mv_ext mysql; }
postgresql() { _mv_ext pgsql; }
cdb() { _mv_ext cdb; }
dbmdb() { _mv_ext dbmdb; }
dnsdb() { _mv_ext dnsdb; }

sha512sums="
eab7ca28b37f1635c48f5e963ab69fcbad539b2c35a84286ecaad7d7ff5210bbefce86452302e08099afdc0710f9cb7ca6d9b152b0ba88a19292f7c5541e0cfc  exim-4.97.1.tar.xz
6ea7670e30815807272d1d9033e75836b883cb2f14f05f0d38aa3f8aecd2516c0763a29f71267b6380f9e606156c889a5c77d444545769af68baaffb80d0dac7  bounce-charset.patch
0599b5140495a563da1e5368045ad9a6fe496c8b519591359f9915bc9d036183ae83584d5e62dd5bd95d6e5554c93483b7968fee8536068b36fd93c4f32dfa25  dnsdb-multi-chunk.patch
198224ca544c2780c5d8106bb74304d871dcfde7d90707291d7e478b8950efe33488accfd896cb86b1a5b4f32ae6040ac0c44907f1b0843ef64037bea55f5e66  exim.Makefile
bb6f5ead067af19ace661cc92bcd428da97570aedd1f9dc5b61a34e7e3fb3e028be6c96d51df73353bdfcaf69a3ee053fb03d245f868d63ebf518aa96ec82d66  exim.confd
3769e74a54566362bcdf57c45fbf7d130d7a7529fbc40befce431eef0387df117c71a5b57779c507e30d5b125913b5f26c9d16b17995521a1d94997be6dc3e02  exim.initd
28e748693a6a72d9943fa9c342ff041fe650fa6977f468dee127e845e6c2a91872ce33fb6f5698838906bde3ed92de7a91cdb0349cedc40b806261867e8c06cb  exim.logrotate
abdaf749ed3947a75b997caa300bf9f27ef82760f1854aa4521a9ac0f322f1655b65a375bc7a709259daea88bf93cfab5289997fa8e376fac9a3477f09bab642  exim.gencert
"
